package com.platform.user.controller;

import com.platform.bean.PageInfo;
import com.platform.bean.Result;
import com.platform.user.entity.User;
import com.platform.user.service.MyAdminDetailService;
import com.platform.user.service.MyUserDetailService;
import com.platform.utils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;

@RestController
@RequestMapping("/userManager")
public class UserManageController {

    @Resource
    private MyAdminDetailService myAdminDetailService;
    @Resource
    private MyUserDetailService myUserDetailService;

    @PostMapping("/queryUserByParams")
    public Result queryUserByParams(@RequestBody Map<String, Object> params) {
        PageInfo<User> userPageInfo = myUserDetailService.queryUserByPage(params);
        return Result.ok(userPageInfo);
    }

    @PostMapping("/addManager")
    public Result addManager(@RequestBody Map<String, Object> params) {
        // Map<String, Object> res = new HashMap<>();
        if (myAdminDetailService.queryAdminByUsername((String) params.get("username")) != null) {
            return Result.fail(403, "用户已经存在", null);
        }
        int i = myAdminDetailService.addAdmin(params);
        return Result.ok(200, "用户添加成功", i);
    }

    @PostMapping("/addUser")
    public Result addUser(@RequestBody Map<String, Object> params) throws IOException {
        // Map<String, Object> res = new HashMap<>();
        if (myUserDetailService.queryUserByUsername((String) params.get("username")) != null) {
            return Result.fail(403, "用户已经存在", null);
        }
        // params.forEach((key, value) -> System.out.println("Key: " + key + ", Value: " + value));
        User user = myUserDetailService.addUser(params);
        return Result.ok(200, "用户添加成功", user);
    }

    @PostMapping("/downloadExcel")
    public void downloadExcelTemplate(HttpServletResponse response) throws IOException {
        // 定义自定义表头
        List<String> customHeaders = new ArrayList<>();
        customHeaders.add("username");
        customHeaders.add("password");
        // customHeaders.add("profile_picture");
        customHeaders.add("full_name");
        customHeaders.add("identity");
        customHeaders.add("sex");
        customHeaders.add("academy");
        customHeaders.add("major");
        customHeaders.add("email");

        ExcelUtils.exportUsers("导入模板", customHeaders, response);
    }

    @PostMapping("importUserExcel")
    public Result importUserExcel(@RequestParam("file") MultipartFile file) throws IOException {
        myUserDetailService.addUserByExcel(file);
        return Result.ok("用户导入成功");
    }

    @PostMapping("deleteUser")
    public Result deleteUser(@RequestBody Map<String, Object> params) {
        myUserDetailService.deleteUserById(Integer.parseInt(params.get("id").toString()));
        return Result.ok("删除用户成功");
    }

    @PostMapping("batchDeleteUser")
    public Result batchDeleteUser(@RequestBody Map<String, Object> params) {
        List<Integer> idList = (List<Integer>) params.get("idList");
        for (int x: idList) {
            // myUserDetailService.deleteUserById(Integer.parseInt(x));
            // System.out.println(x);
            myUserDetailService.deleteUserById(x);
        }
        return Result.ok("批量删除用户成功");
    }
}
